﻿using System;
using System.Collections.Generic;
using Citi.Helpers;
using Citi.RoutineBase;
using System.IO;

namespace Citi.Scheduler.RunnerApplication.SchedulerUI
{
    public class Execution : IExecutableRoutine
    {
        public const string ApplicationPathName = "ApplicationPath";
        public const string LogApplicationPathName = "LogApplicationPath";
        public const string ApplicationArguments = "ApplicationArguments";
        public const string ValidateFile = "ValidateFile";
        public const string UnlockLockMachine = "UnlockLockMachine";

        public IEnumerable<RoutineParameter> Parameters
        {
            get
            {
                return new List<RoutineParameter> 
                { 
                    new RoutineParameter{ Description = "Application Path on Shared Path", Name = ApplicationPathName, Nullable = false, Required= true, Type = typeof(string)},
                    new RoutineParameter{ Description = "Path was the Application logs are written.", Name = LogApplicationPathName, Nullable = true, Required = false, Type = typeof(string)},
                    new RoutineParameter{ Description = "Unlock and lock machine? Only works on Windows XP.", Name = UnlockLockMachine, Nullable = true, Required  = false, Type = typeof(bool)},
                    new RoutineParameter{ Description = "Application arguments", Name = ApplicationArguments, Nullable = true, Required  = false, Type = typeof(string)},
                    new RoutineParameter{ Description = "File to validate if the execution occurred Ok", Name = ValidateFile, Nullable = true, Required  = false, Type = typeof(string)}
                };
            }
        }

        public void Rollback(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            currentAction.Description = "Rolling back";
            var applicationPath = parameters.ContainsKey(ApplicationPathName) ? parameters[ApplicationPathName].ToString() : null;

            if (string.IsNullOrWhiteSpace(applicationPath)) return;

            currentAction.Description = string.Format( "Application Path: {0}",applicationPath);

            SchedulerHelper.RollbackRunner(parameters, applicationPath);
            currentAction.StopLogOnFile();
        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            var applicationPath = parameters.ContainsKey(ApplicationPathName) ? parameters[ApplicationPathName].ToString() : null;
            var logApplicationPath = parameters.ContainsKey(LogApplicationPathName) ? parameters[LogApplicationPathName].ToString() : null;
            var validateFile = parameters.ContainsKey(ValidateFile) ? parameters[ValidateFile].ToString() : null;

            if (string.IsNullOrWhiteSpace(applicationPath))
                throw new Exception(string.Format("The parameter '{0}' was not set.", ApplicationPathName));

            if (!string.IsNullOrWhiteSpace(logApplicationPath))
                currentAction.StartLogOnFile(logApplicationPath);

            SchedulerHelper.StartRunner(parameters, applicationPath);

            currentAction.StopLogOnFile();

            if (validateFile == null) return;

            System.Threading.Thread.Sleep(10000);

            currentAction.Description = string.Format("Validating File: {0}", validateFile);

            if (!File.Exists(validateFile))
            {
                var message = string.Format("File {0} does not exist.", validateFile);
                currentAction.Description = message;
                throw new FileNotFoundException(message);
            }

            currentAction.Description = "File is OK";
        }

        public void Stop(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {

        }
    }
}